import { Component } from '@angular/core';

import { NavController, ViewController, AlertController} from 'ionic-angular';

import { Storage } from '@ionic/storage';

import { Common } from "../../../providers/common";
import { Config } from "../../../providers/config";

import _ from "lodash";
import {CoinService} from "./coin.service";

@Component({
	selector: 'modal-ipm-coin-select-stockno',
	templateUrl: 'selectStockNo.modal.html',
  providers: [CoinService]
})
export class IpmCoinSelectStockNoModal {
	moa_page_name: any = "modal-ipm-coin-select-stockno";
  moa_page_explain: any = "货币基金试算 - 选择债券代码";

  dataList = [];
  dataListSource = [];

  isSelectAll = false;

  isLoadMore = true;

  keyWord = "";

  pageIndex = 1;

  marketNo = "";
  stockClass = "";
	constructor(
		public navCtrl: NavController,
    public common: Common,
    public viewCtrl: ViewController,
    public storage: Storage,
    public coinService: CoinService,
    public alertCtrl: AlertController,
    public config: Config
	) {
    this.marketNo = this.viewCtrl.getNavParams().get("marketNo");
    this.stockClass = this.viewCtrl.getNavParams().get("stockClass");
    this.keyWord = this.viewCtrl.getNavParams().get("stockCode");
  }

  updateList() {
	  if (this.keyWord) {
	    this.dataList = _.filter(this.dataList, data => {
	      return data["entry"]["province_city"].indexOf(this.keyWord) > -1;
      });
    } else {
	    this.dataList = this.dataListSource;
    }
  }

  selectAll() {
	  if (this.isSelectAll) {
      _.forEach(this.dataList, fund => {
        fund["selected"] = true;
      });
    } else {
      _.forEach(this.dataList, fund => {
        fund["selected"] = false;
      });
    }
  }

  ionViewDidEnter() {
    this.loadListData(this.pageIndex, null, null);
  }

  loadListData(page, infiniteScroll, refresher) {
    this.coinService.getStockNoList(this.marketNo, this.stockClass, this.keyWord, page).subscribe(stockNoList => {

      if (stockNoList) {

        // 如果加载第一页，则清空数据
        if (page == 1) {
          this.isLoadMore = true;
          this.dataList = [];
        }

        this.dataList = _.concat(this.dataList, stockNoList);

        // 增加搜索索引
        this.pageIndex = this.pageIndex + 1;
        // 如果返回数据少于请求页数，则不再加载更多
        if (stockNoList.length < this.config.pageSize) {
          this.isLoadMore = false;
        }
      } else {
        this.dataList = [];
        this.isLoadMore = false;
      }

      // complete event
      if (infiniteScroll) {
        infiniteScroll.complete();
      }
      if (refresher) {
        refresher.complete();
      }
    });
  }

  closeModal() {
    this.viewCtrl.dismiss("close");
  }

  search() {
    this.pageIndex = 1;
    this.loadListData(this.pageIndex, null, null);
  }

  radioSelected(data) {
	  this.viewCtrl.dismiss(data);
  }

  /**
   * [doInfinite 加载更多]
   * @param {[type]} infiniteScroll [事件]
   */
  doInfinite(infiniteScroll) {
    if (this.pageIndex != 1) {
      if (this.isLoadMore) {
        this.loadListData(this.pageIndex, infiniteScroll, null);
      } else {
        infiniteScroll.complete();
      }
    }
  }
  /**
   * [doRefresh 下拉刷新]
   * @param {[type]} refresher [事件]
   */
  doRefresh(refresher) {
    this.pageIndex = 1;
    this.isLoadMore = true;
    this.loadListData(this.pageIndex, null, refresher);
  }

}

